//
//  QPlayBoxView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/4.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QPlayBoxView.h"
#import "QPlayBoxSubView.h"
//#import "QShowSignView.h"

const CGFloat pw = 220;
const CGFloat ph = 265;

@interface QPlayBoxView()

@end
@implementation QPlayBoxView{

    UIControl *_maskView;
    QPlayBoxSubView *_subView;
    //QShowSignView *_signView;
}

- (instancetype)initWithStyle:(PlayboxViewStyle)style{
    self = [super init];
    if (self) {
        _playStyle = style;
        self.backgroundColor = [UIColor whiteColor];
        self.frame = [UIScreen mainScreen].bounds;
        [self configViews];
    }
    return self;
}



//杨凯
- (instancetype)initWithName:(NSString *)name Number:(NSString *)number{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = [UIScreen mainScreen].bounds;
        [self configViewsWithName:name Number:number];
    }
    return self;
}
//杨凯
- (void)configViewsWithName:(NSString *)name Number:(NSString *)number{
    
    _maskView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _maskView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    [_maskView addTarget:self action:@selector(maskAction) forControlEvents:UIControlEventTouchUpInside];
    _maskView.hidden = YES;
    [self addSubview:_maskView];
    
    _subView = [[[NSBundle mainBundle] loadNibNamed:@"QPlayBoxSubView" owner:self options:nil] lastObject];
    _subView.frame = CGRectMake((kScreenWidth-pw)/2, kScreenHeight, pw, ph);
    _subView.subStyle = _playStyle;
    __block QPlayBoxView *weakSelf = self;
    _subView.cancelBlock = ^(){
        [weakSelf hidePlayBoxView];
        if (weakSelf.ChooseIndexBlock) {
            weakSelf.ChooseIndexBlock(0);
        }
        CLog(@"取消");
        
    };
    _subView.agreeBlock = ^(){
        [weakSelf hidePlayBoxView];
        if (weakSelf.ChooseIndexBlock) {
            weakSelf.ChooseIndexBlock(1);
        }
        CLog(@"确定");
    };
    [_maskView addSubview:_subView];
    
    _subView.iconName = @"callPhoneicon_image";
    _subView.titleName = name;
    _subView.subTitleName = number;
    
}



- (void)configViews{
    
    _maskView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _maskView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    [_maskView addTarget:self action:@selector(maskAction) forControlEvents:UIControlEventTouchUpInside];
    _maskView.hidden = YES;
    [self addSubview:_maskView];
    
    _subView = [[[NSBundle mainBundle] loadNibNamed:@"QPlayBoxSubView" owner:self options:nil] lastObject];
    _subView.frame = CGRectMake((kScreenWidth-pw)/2, kScreenHeight, pw, ph);
    _subView.subStyle = _playStyle;
    __block QPlayBoxView *weakSelf = self;
    _subView.cancelBlock = ^(){
        [weakSelf hidePlayBoxView];
        if (weakSelf.ChooseIndexBlock) {
            weakSelf.ChooseIndexBlock(0);
        }
        CLog(@"取消");

    };
    _subView.agreeBlock = ^(){
        [weakSelf hidePlayBoxView];
        if (weakSelf.ChooseIndexBlock) {
            weakSelf.ChooseIndexBlock(1);
        }
        CLog(@"确定");
    };
    [_maskView addSubview:_subView];
    
    switch (_playStyle) {
        case PlayboxCallPhoneStyle://打电话的提示
        {
            _subView.iconName = @"callPhoneicon_image";
            _subView.titleName = @"客服电话";
            _subView.subTitleName = @"400-8916-577";
        }
            break;
        case PlayboxChangeMemberStyle://加入会员的提示
        {
            _subView.iconName = @"changeMember_image";
            _subView.titleName = @"您还没有登录哦";
            _subView.subTitleName = @"";

        }
            break;
        case PlayboxcancelOrderStyle://取消订单的提示
        {
            _subView.iconName = @"cancelOrder_image";
            _subView.titleName = @"确定取消订单?";
            _subView.subTitleName = @" ";
        }
            break;
        case PlayboxVersionupdateStyle://取消订单的提示
        {
            _subView.iconName = @"versionUpdate_image";
            _subView.titleName = @"有新版本哦";
            _subView.subTitleName = @" ";
        }
            break;
        case playboxPeoplePayOrderStyle://代付款
        {
        
            _subView.iconName = @"callPhoneicon_image";
            _subView.titleName = @"确定拨打电话提醒对方支付吗?";
            _subView.subTitleName = @"";

        }
        case PlayboxCallDriverPhone:
        {
            
            _subView.iconName = @"callPhoneicon_image";
            _subView.titleName = @"司机电话";
            _subView.subTitleName = @"";
            
        }
            break;
        default:
            break;
    }
}

- (void)maskAction{
    if(_playStyle == PlayboxVersionupdateStyle){
        return;
    }else{
        [self hidePlayBoxView];
    }
}

- (void)showPlayBoxView{
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    
    [window addSubview:_maskView];
    _maskView.hidden = NO;
    _maskView.alpha = 0;
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        _subView.center = self.center;
        _maskView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hidePlayBoxView{
    _maskView.alpha = 1;
    [UIView animateWithDuration:.3 animations:^{
        _subView.frame = CGRectMake((kScreenWidth-pw)/2, kScreenHeight, pw, ph);
        _maskView.alpha = 0;
    } completion:^(BOOL finished) {
        _maskView.hidden = YES;
        [_maskView removeFromSuperview];
        [_subView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

@end
